from gradio_client import Client
import pygame
from pygame.mixer import Sound

class AiTTs:
    def __init__(self):
        self.log = []
        self.ttsPath = []
        self.client = Client("http://127.0.0.1:7860/")
        self.LoadModel()
        pygame.mixer.init()
    def LoadModel(self):

        #预加载模型
        result = self.client.predict('G_taffy2.pth',# ckpt_name
                    '聚类模型.pt',# cluster_name
                    '配置文件.json',# config_name
                    False,# enhance = False,
                    '扩散模型.pt',# diff_model_name
                    '扩散模型配置文件.yaml',# diff_config_name
                    False,# only_diffusion
                    False,# use_spk_mix
                    'Auto',# using_device
                    "dpm-solver++",# method
                    10,# speedup
                    0,# cl_num
                    'nsf_hifigan_finetuned',# vocoder_name
                    api_name="/modelAnalysis")
        self.loadLog(result)
        self.outputWord(self.log[-1])
    def outputWord(self,word):
        print(word)
        #返回的信息
    def loadLog(self,information):
        self.log.append(information)
        #记录信息
    def loadTTsPath(self,Path):
        self.ttsPath.append(Path)
        #记录创建音频的位置

    def OwnTTs(self,talk="hello"):
        change = self.client.predict(
            talk, "女", "Auto", 0, 0, "wav", "acetaffy", 0, False, 0, -50, 0.4, 0.5, 0, 1, 0.75, "rmvpe", 0, 0.05, 400,
            False, False, 0,
            api_name="/vc_fn2",
        )
        #载入预测模型，创建音频
        self.loadLog(change)
        #记录信息，便于查找
        self.loadTTsPath(change[1])
        #记录创建音频的位置

    def ttsTalk(self,path):
        sound = Sound(path)
        sound.play()
        while pygame.mixer.get_busy():
            pygame.time.Clock().tick(10)
        #播放音频

    def RunTTs(self,word):
        self.OwnTTs(word)
        self.ttsTalk(self.ttsPath[-1])



# ai = AiTTs()
# ai.RunTTs("早上好，今天也是美好的一天。")
# ai.RunTTs("中午好，中午想吃点什么？")
# ai.RunTTs("晚上好，晚上有什么安排吗？")
